define(["angularAMD"], function (angularAMD) {
    angularAMD.service('searchDialogService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', 'RouterExtendProvider',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $RouterExtendProvider){
            this.init = function(options, callback){
                let openDialog = () => {
                    $mdDialog.dialog(
                        'module/system/program/searchDialog/searchDialog.html',
                        controller
                    );
                }
                // 20220418 modify by Alan for#110059 : 移除未使用的程式碼
                // 20220614 modify by Alan for#112054 : 關閉dialog後重設選擇項目
                let controller = (dialog) => {
                    return {
                        css_url: 'module/system/program/searchDialog/searchDialog.css',
                        item: options.item,
                        searchValue: '',
                        tempValue: '',
                        list: getListFrom($rootScope.menu),
                        background: {
                            click() {
                                dialog.back();
                            }
                        },
                        executeProgram(item) {
                            dialog.back();
                            $RouterExtendProvider.goToProgram(item);
                        },
                        search() {
                            dialog.searchValue = dialog.tempValue;
                        },
                        back() {
                            dialog.hide();
                            dialog.unSelectModule();
                            callback && callback(dialog);
                        },
                        unSelectModule() {
                            $rootScope.selectModule.name = '';
                        }
                    };
                }
                
                // 20220418 modify by Alan for#110059 : 新增{isShow:true}過濾掉不顯示的作業/報表
                let getListFrom = (menu) => {
                    var list = [];
                    menu = $filter('filter')(menu, { isShow: true });
                    menu.forEach((module) => {
                        // 20230223 modify by Alan for#123998 : 看板需要額外處理
                        if (module.name == 'C20' || module.name == 'modelKB') {
                            // 20230524 modify by Alan for#0135796 : 修正會跳出"尚未設定自定義看板"的錯誤
                            $RouterExtendProvider.loadKanbanModuleNew(module, () => {
                                addProgram(list, module);
                            });
                        } else if (module.name == 'C01' || module.name == 'modelC') {
                            $RouterExtendProvider.loadReportModuleNew(module, () => {
                                addProgram(list, module);
                            });
                        } else {
                            addProgram(list, module);
                        }
                    })
                    return list
                }

                function addProgram(list, module) {
                    if (module.hasOwnProperty('childs')) {
                        var filtedModules = $filter('filter')(module.childs, { isShow: true });
                        filtedModules.forEach((programs) => {
                            var editedPrograms = angular.copy(programs);
                            editedPrograms.translate = $filter('translate')(editedPrograms.title, { isShow: true });
                            list.push(editedPrograms);
                        })
                    }
                }

                openDialog();
            };
        }]);
});
